<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>初始化 state - 紫霞仙子</title>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        function test(){
            console.log(this);
        }


        //创建虚拟 DOM
        class App extends React.Component{
            
            //state 属性声明 方式二
            state = {
                isNight: true
            }

            //声明构造方法  this
            constructor(){  
                super();
                this.switchDay = this.switchDay.bind(this);
            }

            render(){
                //render 方法中 this 是指向实例对象的
                return <div>
                        <h2>至尊宝, 我是{this.state.isNight ? '青霞仙子' : '紫霞仙子'}  
                            <button className="abc" onClick={this.switchDay}>切换白天与黑夜</button>
                        </h2>    
                    </div>
            }

            switchDay(){
                //修改 isNight 属性的值
                // this.state.isNight = false; // X
                //调用 this.setState  set 设置  state 状态
                this.setState({
                    isNight: !this.state.isNight 
                })
            }
        }

        //渲染
        ReactDOM.render(<App />, document.querySelector("#root"));


    </script>
</body>
</html>